package com.agritech.machine;

import com.agritech.state.IdleState;
import com.agritech.state.MachineState;

public class AgriculturalMachineContext {
    private MachineState currentState;
    private String machineId;
    private String type; // 农机类型，例如播种机、收割机
    private double fuelLevel = 100.0; // 初始油量
    private String location = "仓库"; // 初始位置

    public AgriculturalMachineContext(String machineId, String type) {
        this.machineId = machineId;
        this.type = type;
        // 初始状态为空闲
        this.currentState = new IdleState(); 
        System.out.println("农机 [" + machineId + " 类型: " + type + "] 已创建，初始状态: " + currentState.getStateName());
    }

    public void setState(MachineState state) {
        System.out.println("农机 [" + machineId + "] 状态从 [" + this.currentState.getStateName() + "] 切换到 [" + state.getStateName() + "]");
        this.currentState = state;
    }

    public MachineState getCurrentState() {
        return currentState;
    }

    public void start() {
        currentState.start(this);
    }

    public void stop() {
        currentState.stop(this);
    }

    public void work() {
        currentState.work(this);
    }

    public void fix() {
        currentState.fix(this);
    }

    public String getMachineId() {
        return machineId;
    }

    public String getType() {
        return type;
    }

    public double getFuelLevel() {
        return fuelLevel;
    }

    public void setFuelLevel(double fuelLevel) {
        this.fuelLevel = Math.max(0, Math.min(100, fuelLevel)); // 保证油量在0-100之间
        System.out.println("农机 [" + machineId + "] 油量更新为: " + this.fuelLevel + "%");
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
        System.out.println("农机 [" + machineId + "] 位置更新为: " + this.location);
    }

    public void displayStatus() {
        System.out.println("--- 农机 [" + machineId + "] (" + type + ") 状态报告 ---");
        System.out.println("当前状态: " + currentState.getStateName());
        System.out.println("位置: " + location);
        System.out.println("油量: " + String.format("%.1f", fuelLevel) + "%");
        System.out.println("---------------------------------------");
    }
}